//-----------------Sonic CD DrillBot Script-------------------//
// Yeah...

// Aliases
#alias Object.Value0		:	Object.Timer
#alias Object.Value1		:	Object.ExtraTimer
#alias Object.Value2		:	Object.JumpBlock
#alias Object.Value3		:	Object.XVelocity
#alias Object.Value4		:	Object.YVelocity
#alias Object.Value5		:	Object.XOriginPos
#alias Object.Value6		:	Object.YOriginPos

#alias Object.Value4	:	SnakeBullet.XVelocity

#alias Object.PropertyValue	:	Object.Quality

// States
#alias 0	:	SNAKE_IDLE
#alias 1	:	SNAKE_UP
#alias 2	:	SNAKE_UP2
#alias 3	:	SNAKE_SHOOT
#alias 4	:	SNAKE_DOWN
#alias 5	:	SNAKE_COOLDOWN
#alias 6	:	SNAKE_DOWN2

// Collision Sides
#alias 0	:	CSIDE_FLOOR
#alias 1	:	CSIDE_LWALL
#alias 2	:	CSIDE_RWALL

// Badnik Quality / Property Values
#alias 0	:	GOOD_QUALITY
#alias 1	:	BAD_QUALITY

// Priority
#alias 0	:	PRIORITY_BOUNDS
#alias 1	:	PRIORITY_ACTIVE

sub ObjectMain
	CallFunction(StageSetup_CheckGoodFuture)	// Check if this should be a flower
	if Object.Quality == GOOD_QUALITY
		switch Object.State
		case SNAKE_IDLE
			if Player.XPos >= Object.XPos
				Object.Direction = FACING_LEFT
			else
				Object.Direction = FACING_RIGHT
			end if
			Object.YVelocity = -350000
			break

		case SNAKE_UP
			Object.YVelocity -= 40000
			Object.YPos += Object.YVelocity
			if Object.YVelocity <= -590000
				Object.State = SNAKE_UP2
				Object.YPos += -200000
			end if
			break

		case SNAKE_UP2
			if Player.XPos >= Object.XPos
				Object.Direction = FACING_LEFT
			else
				Object.Direction = FACING_RIGHT
			end if
			Object.Timer++
			if Object.Timer >= 6
				Object.State = SNAKE_SHOOT
				Object.Timer = 30
			end if
			break

		case SNAKE_SHOOT
			Object.ExtraTimer++
			Object.Timer--
			if Object.Timer == 0
				PlayStageSfx(2, false)
				TempValue1  = Object.XPos

				TempValue2  = Object.YPos
				TempValue2 += -0x20000

				if Object.Direction == FACING_LEFT
					TempValue1 += 0x96000
				else
					TempValue1 += -0x96000
				end if

				CreateTempObject(TypeName[SnakeBullet], TempValue0, TempValue1, TempValue2)
				Object[TempObjectPos].DrawOrder =  3
				
				if Object.Direction == FACING_LEFT
					SnakeBullet[TempObjectPos].XVelocity =  0x25000
				else
					SnakeBullet[TempObjectPos].XVelocity =  -0x25000
				end if

				TempValue0++
			end if
				if Object.ExtraTimer == 120
					Object.ExtraTimer = 0
					Object.Timer = 0
					Object.State = SNAKE_DOWN2
					Object.YVelocity = 350000
				end if
			break

		case SNAKE_DOWN2
			Object.ExtraTimer++
				if Object.ExtraTimer == 6
					Object.ExtraTimer = 0
					Object.State = SNAKE_DOWN
				end if
			break
		
		case SNAKE_DOWN
			if Object.YVelocity > 590000
				Object.YPos += -400000
				Object.State = SNAKE_COOLDOWN
				Object.Timer = 0
			else
				Object.YVelocity += 40000
				Object.YPos += Object.YVelocity
			end if
			break

		case SNAKE_COOLDOWN
			Object.Timer++
			if Object.Timer >= 60
				Object.YPos -= 30000
				Object.State = SNAKE_IDLE
				Object.Timer = 0
			end if
			break

		end switch
	else
		switch Object.State
		case SNAKE_IDLE
			if Player.XPos >= Object.XPos
				Object.Direction = FACING_LEFT
			else
				Object.Direction = FACING_RIGHT
			end if
			Object.YVelocity = -120000
			break

		case SNAKE_UP
			Object.YVelocity -= 20000
			Object.YPos += Object.YVelocity
			if Object.YVelocity <= -340000
				Object.State = SNAKE_UP2
				Object.YPos += -200000
			end if
			break

		case SNAKE_UP2
			if Player.XPos >= Object.XPos
				Object.Direction = FACING_LEFT
			else
				Object.Direction = FACING_RIGHT
			end if
			Object.Timer++
			if Object.Timer >= 12
				Object.State = SNAKE_SHOOT
				Object.Timer = 1
			end if
			break

		case SNAKE_SHOOT
			Object.ExtraTimer++
				if Object.ExtraTimer == 120
					Object.ExtraTimer = 0
					Object.Timer = 0
					Object.State = SNAKE_DOWN2
					Object.YVelocity = 120000
				end if
			break

		case SNAKE_DOWN2
			Object.ExtraTimer++
				if Object.ExtraTimer == 6
					Object.ExtraTimer = 0
					Object.State = SNAKE_DOWN
				end if
			break
			
		case SNAKE_DOWN
			if Object.YVelocity > 350000
				Object.YPos += -100000
				Object.State = SNAKE_COOLDOWN
				Object.Timer = 0
			else
				Object.YVelocity += 20000
				Object.YPos += Object.YVelocity
			end if
			break

		case SNAKE_COOLDOWN
			Object.Timer++
			if Object.Timer >= 60
				Object.YPos -= 60000
				Object.State = SNAKE_IDLE
				Object.Timer = 0
			end if
			break

		end switch
	end if
end sub


sub ObjectPlayerInteraction
	switch Object.State
	case SNAKE_IDLE
		PlayerObjectCollision(C_TOUCH, -128, -148, 128, -20) // Left Detect
		if CheckResult == true
			Object.Timer++
			if Object.Timer >= 6
				Object.State = SNAKE_UP
				Object.Timer = 0
			end if
		end if
		break

	case SNAKE_UP
	case SNAKE_UP2
	case SNAKE_SHOOT
	case SNAKE_DOWN
	case SNAKE_DOWN2
		PlayerObjectCollision(C_TOUCH, -16, -16, 16, 24) // Left Detect
		if CheckResult == true
			CallFunction(Player_BadnikBreak)
		end if
		break
	end switch
end sub

sub ObjectDraw
	if Object.Quality == GOOD_QUALITY
		switch Object.State
		case SNAKE_IDLE
		case SNAKE_UP
		case SNAKE_DOWN
			DrawSpriteFX(0, FX_FLIP, Object.XPos, Object.YPos)
			break
		case SNAKE_UP2
		case SNAKE_DOWN2
			DrawSpriteFX(1, FX_FLIP, Object.XPos, Object.YPos)
			break
		case SNAKE_SHOOT
			DrawSpriteFX(2, FX_FLIP, Object.XPos, Object.YPos))
			break
		end switch
	else
		switch Object.State
		case SNAKE_IDLE
		case SNAKE_UP
		case SNAKE_DOWN
			DrawSpriteFX(3, FX_FLIP, Object.XPos, Object.YPos)
			break
		case SNAKE_UP2
		case SNAKE_DOWN2
			DrawSpriteFX(4, FX_FLIP, Object.XPos, Object.YPos)
			break
		case SNAKE_SHOOT
			DrawSpriteFX(5, FX_FLIP, Object.XPos, Object.YPos))
			break
		end switch
	end if
end sub

sub ObjectStartup
	LoadSpriteSheet("R2/Objects.gif")

	// Good Quality
	SpriteFrame(-16, -15, 32, 40, 1, 124)			// #1 - Up 2
	SpriteFrame(-16, -22, 32, 46, 1, 165)			// #0 - Up 1
	SpriteFrame(-16, -15, 32, 40, 1, 83)			// #2 - Shoot
	// Bad Quality
	SpriteFrame(-16, -20, 32, 40, 34, 124)			// #1 - Up 2
	SpriteFrame(-16, -24, 32, 46, 34, 165)			// #0 - Up 1
	SpriteFrame(-16, -20, 32, 40, 34, 83)			// #2 - Shoot
	
	// Used to be below LoadSpriteSheet, moved here for consistency
	ArrayPos0 = 32
	while ArrayPos0 < 1056
		if Object[ArrayPos0].Type == TypeName[Drill Bot]
			Object[ArrayPos0].XOriginPos = Object[ArrayPos0].XPos
			Object[ArrayPos0].YOriginPos = Object[ArrayPos0].YPos
		end if
		ArrayPos0++
	loop
end sub

// ========================
// Editor Subs
// ========================

sub RSDKEdit
	if editor.returnVariable == true
		switch editor.variableID
		case EDIT_VAR_PROPVAL // property value
			checkResult = object.propertyValue
			break
		case 0 // Sprite
			checkResult = object.propertyValue
			break
		end switch
	else
		switch editor.variableID
		case EDIT_VAR_PROPVAL // property value
			object.propertyValue = editor.variableValue
			break
		case 0 // Sprite
			object.propertyValue = editor.variableValue
			break
		end switch
	end if
end sub

#alias 0	:	INK_NONE
#alias 1	:	INK_BLEND
#alias 2	:	INK_ALPHA
#alias 3	:	INK_ADD
#alias 4	:	INK_SUB


sub RSDKDraw
	if editor.showGizmos == true
		editor.drawingOverlay = true

		TempValue0 = object.ypos
		TempValue0 -= 0x350000
		DrawArrow(object.xpos, object.ypos, object.xpos, TempValue0, 255, 255, 255)
		
		object.inkEffect = INK_BLEND
		DrawSpriteFX(object.propertyValue, FX_INK, object.xpos, TempValue0)
		
		editor.drawingOverlay = false
	end if

	DrawSprite(object.propertyValue)
end sub


sub RSDKLoad
	LoadSpriteSheet("R2/Objects.gif")
	SpriteFrame(-16, -15, 32, 40, 1, 83)			// #0 - Shoot
	SpriteFrame(-16, -15, 32, 40, 34, 83)			// #1 - Shoot

	SetVariableAlias(ALIAS_VAR_PROPVAL, "condition")

	AddEditorVariable("condition")
	SetActiveVariable("condition")

	AddEnumVariable("Good", 0)
	AddEnumVariable("Bad", 1)
end sub